package cn.iocoder.yudao.module.bk.controller.admin.lwbook;

import cn.iocoder.yudao.module.bk.controller.app.vo.BookInfoReq;
import cn.iocoder.yudao.module.bk.controller.app.vo.IsbnReq;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.time.LocalDateTime;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;

import cn.iocoder.yudao.module.bk.controller.admin.lwbook.vo.*;
import cn.iocoder.yudao.module.bk.dal.dataobject.lwbook.LwBookDO;
import cn.iocoder.yudao.module.bk.service.lwbook.LwBookService;

@Tag(name = "管理后台 - 来往书籍")
@RestController
@RequestMapping("/bk/lw-book")
@Validated
public class LwBookController {

    @Resource
    private LwBookService lwBookService;

    @PostMapping("/create")
    @Operation(summary = "创建来往书籍")
    @PreAuthorize("@ss.hasPermission('bk:lw-book:create')")
    public CommonResult<Long> createLwBook(@Valid @RequestBody LwBookSaveReqVO createReqVO) {
        return success(lwBookService.createLwBook(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新来往书籍")
    @PreAuthorize("@ss.hasPermission('bk:lw-book:update')")
    public CommonResult<Boolean> updateLwBook(@Valid @RequestBody LwBookSaveReqVO updateReqVO) {
        lwBookService.updateLwBook(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除来往书籍")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bk:lw-book:delete')")
    public CommonResult<Boolean> deleteLwBook(@RequestParam("id") Long id) {
        lwBookService.deleteLwBook(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得来往书籍")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bk:lw-book:query')")
    public CommonResult<LwBookRespVO> getLwBook(@RequestParam("id") Long id) {
        LwBookDO lwBook = lwBookService.getLwBook(id);
        return success(BeanUtils.toBean(lwBook, LwBookRespVO.class));
    }

    @PostMapping("/getIsbn")
    @Operation(summary = "ISBN获得书籍信息")
    @PreAuthorize("@ss.hasPermission('bk:lw-book:query')")
    public CommonResult<BookInfoReq> getIsbn(@RequestBody IsbnReq req) {
        return lwBookService.isbnSearch(req);
    }

    @PostMapping("/getIsbnByApi")
    @Operation(summary = "ISBN获得书籍信息-API")
    @PreAuthorize("@ss.hasPermission('bk:lw-book:query')")
    public CommonResult<BookInfoReq> getIsbnByApi(@RequestBody IsbnReq req) {
        return lwBookService.isbnSearchByApi(req);
    }

    @GetMapping("/page")
    @Operation(summary = "获得来往书籍分页")
    @PreAuthorize("@ss.hasPermission('bk:lw-book:query')")
    public CommonResult<PageResult<LwBookRespVO>> getLwBookPage(@Valid LwBookPageReqVO pageReqVO) {
        PageResult<LwBookDO> pageResult = lwBookService.getLwBookPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LwBookRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出来往书籍 Excel")
    @PreAuthorize("@ss.hasPermission('bk:lw-book:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLwBookExcel(@Valid LwBookPageReqVO pageReqVO,
                                  HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LwBookDO> list = lwBookService.getLwBookPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "来往书籍.xls", "数据", LwBookRespVO.class,
                BeanUtils.toBean(list, LwBookRespVO.class));
    }

    @GetMapping("/statis-data")
    @Operation(summary = "统计数据")
    public CommonResult<StatisDataRes> statisData(StatisDataReq req) {
        return lwBookService.statisData(req);
    }

}
